package org.xfy.org.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xfy.base.service.impl.BaseServiceImpl;
import org.xfy.base.util.AjaxResult;
import org.xfy.base.util.EmailUtil;
import org.xfy.base.util.ErrorCode;
import org.xfy.base.util.examine.TextCensor;
import org.xfy.org.domain.Employee;
import org.xfy.org.domain.Logininfo;
import org.xfy.org.domain.Shop;
import org.xfy.org.domain.ShopAuditLog;
import org.xfy.org.domain.dto.EmployeeDto;
import org.xfy.org.domain.dto.ShopDto;
import org.xfy.org.domain.dto.ShopRejectDto;
import org.xfy.org.domain.vo.EchartsVo;
import org.xfy.org.exception.BusinessException;
import org.xfy.org.mapper.EmployeeMapper;
import org.xfy.org.mapper.LogininfoMapper;
import org.xfy.org.mapper.ShopAuditLogMapper;
import org.xfy.org.mapper.ShopMapper;
import org.xfy.org.service.IShopService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ShopImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private TextCensor textCensor;
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public AjaxResult settlement(ShopDto shopDto) {
        //百度AI审核
        if (textCensor.TextCensor(shopDto.getName())) {
            throw new BusinessException(ErrorCode.Besiness_SHOP_NAME_ERROR.getCode());
        }
        if (textCensor.TextCensor(shopDto.getAddress())) {
            throw new BusinessException(ErrorCode.Besiness_SHOP_ADRESS_ERROR.getCode());
        }
        //校验
        EmployeeDto admin = shopDto.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BusinessException(ErrorCode.Besiness_REGISTER_ERROR.getCode());
        }
        Employee employee = employeeMapper.findOneByphone(admin.getPhone());
        if (employee != null) {
            throw new BusinessException(ErrorCode.Besiness_REGISTER_phone_ERROR.getCode());
        }
        Shop shop1 = shopMapper.findByNameAndAddress(shopDto);
        if (shop1 != null) {
            throw new BusinessException(ErrorCode.Besiness_REGISTER_shop_ERROR.getCode());
        }
        //保存员工信息
        Employee employee1 = new Employee();
        BeanUtils.copyProperties(admin, employee1);
        String salt = RandomUtil.randomString(32); //生成32位的盐值
        String md5Pwd = SecureUtil.md5(admin.getPassword() + salt); //加密加盐
        employee1.setSalt(salt);
        employee1.setPassword(md5Pwd);
        employeeMapper.add(employee1);
        //保存店铺的信息
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopDto, shop);
        shop.setAdminId(employee1.getId());
        shopMapper.add(shop);
        employee1.setShopId(shop.getId());
        employeeMapper.update(employee1);
        return AjaxResult.me();
    }

    /**
     * 店铺审核驳回
     *
     * @param shopRejectDto
     * @return
     */
    @Override
    @Transactional
    public AjaxResult reject(ShopRejectDto shopRejectDto) {
        //更改店铺状态
        Shop one = shopMapper.findOne(shopRejectDto.getShopId());
        one.setState(4);
        shopMapper.update(one);
        //获取用户邮箱
        Employee employee = employeeMapper.findOne(one.getAdminId());
        emailUtil.sendEmail("店铺激活失败", shopRejectDto.getNote(), employee.getEmail());
        //记录日志
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setShopId(shopRejectDto.getShopId());
        shopAuditLog.setState(4);
        shopAuditLog.setNote(shopRejectDto.getNote());
        shopAuditLogMapper.add(shopAuditLog);

        return AjaxResult.me();
    }

    @Override
    @Transactional
    public AjaxResult echarts() {
        List<EchartsVo> list = shopMapper.echarts();
        Map<String, Object> map = new HashMap<>();
        List<Integer> CountNum = list.stream().map(EchartsVo::getCountNum).collect(Collectors.toList());
        List<String> time = list.stream().map(EchartsVo::getTime).collect(Collectors.toList());
        map.put("y", CountNum);
        map.put("x", time);
        return AjaxResult.me().setResultObj(map);
    }

    @Override
    @Transactional
    public AjaxResult pass(ShopRejectDto shopRejectDto) {
        Long shopId = shopRejectDto.getShopId();
        //更改店铺状态
        Shop one = shopMapper.findOne(shopId);
        one.setState(2);
        shopMapper.update(one);
        //获取用户邮箱
        Employee employee = employeeMapper.findOne(one.getAdminId());
        String url = "<a href='http://127.0.0.1:8080/shop/activation/" + shopId + "'>点我激活</a>";
        emailUtil.sendEmail("店铺激活成功", shopRejectDto.getNote() + url, employee.getEmail());
        //记录日志
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setShopId(shopRejectDto.getShopId());
        shopAuditLog.setState(2);
        shopAuditLog.setNote(shopRejectDto.getNote());
        shopAuditLogMapper.add(shopAuditLog);
        return AjaxResult.me();
    }

    @Override
    @Transactional
    public AjaxResult activation(Long shopId) {
        //更改店铺状态
        Shop one = shopMapper.findOne(shopId);
        one.setState(3);
        shopMapper.update(one);
        //获取用户信息，更改状态
        Employee employee = employeeMapper.findOne(one.getAdminId());
        employee.setState(1);
        employeeMapper.update(employee);
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);
        return AjaxResult.me();
    }
}
